<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3"></script>
</head>
<body>
    <div id="app">
        <h1>知识点1：迭代普通数组</h1>
            <ul>
                <li v-for="item in times">桃花</li>
            </ul>
            <ul>
                <li v-for="item in flowers">{{item}}</li>
            </ul>
            <ul>
                <li v-for="(item, index) in flowers">{{index}} -- {{item}}</li>
            </ul>

        <h1>知识点2：迭代对象数组</h1>
            <ul>
                <li v-for="(item,index) in users">{{index}}--{{item.id}}--{{item.name}}</li>
            </ul>

        <h1>知识点3：迭代数字</h1>
            <ul>
                <li v-for="list in 10">{{list}}</li>
            </ul>

        <h1>知识点4：关于key属性</h1>
            ID:<input type="text" v-model="id"><br>
            NAME:<input type="text" v-model="name"><br>
            <button @click="add()">add</button>
            <button @click="del()">delete</button><br>
            我新添加的id是{{id}}，我的新添加的name是{{name}}<br>
            <table border="1">
                <tr v-for="(item,index) in users" :key="item.index">
                    <td><input type="checkbox"></td>
                    <td>{{item.id}}</td>
                    <td>{{item.id}}</td>
                </tr>
            </table>
            <h3>为了提升循环性能，我们一般会给每个循环加上一个唯一的key值，
                在实际中后端会向前端返回数据时，一般会携带唯一的与数据相关的
                数据条目标识符，通常是一个随机字段，我们在写v-for的时候，
                要加key值
            </h3>
            <br>

        <h1>知识点5:v-for循环嵌套</h1>
            <ul>
                <li v-for="(item,index) in users">用户{{index+1}}
                    <div>
                        <p v-for="(list, i) in item">{{i}}:{{list}}</p>
                    </div>
                </li>
            </ul>

        
    </div>  
</body>
</html>

<script>
    const appConn = Vue.createApp({
        data() {
            return {
                times:5,
                flowers:['桃花','梅花','菊花','樱花'],
                users:[{id:'001',name:'张成辉'},{id:'002',name:'被抓住了'}],
                id:'',
                name:'',
            }
        },
        methods: {
            add(){
                //push尾部,unshift在头部插入
                // this.users.push({id:this.id, name:this.name})
                this.users.unshift({id:this.id, name:this.name})
            },
            del(){
                //pop，shift
                // this.users.pop({id:this.id, name:this.name})
                this.users.shift({id:this.id, name:this.name})
            }
        },
    }).mount("#app")

</script>